package scaladuct.reflect

import scaladuct.{Extensible, Property}
import java.lang.Class

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Dec 14, 2010
 * Time: 3:17:21 PM
 */

trait CookieReflection {
  def propertyInfo[X](property: Property[X]): PropertyInfo[X]
}

trait PropertyInfo[X] extends Extensible {
  def name: String

  def clazz: Class[X]

  def mandatory: Boolean

  def enabled: Boolean
}

object DefaultPropertyInfo {
  def apply[X](property: Property[X]): DefaultPropertyInfo[X] = apply(property, true, true)

  def apply[X](property: Property[X], mandatory: Boolean, enabled: Boolean): DefaultPropertyInfo[X] = {
    new DefaultPropertyInfo[X](property.name, property.clazz, mandatory, enabled)
  }
}
class DefaultPropertyInfo[X](val name: String,
                             val clazz: Class[X],
                             val mandatory: Boolean,
                             val enabled: Boolean
        ) extends PropertyInfo[X]

class PropertyInfoDecorator[X](propertyInfo: PropertyInfo[X]) extends PropertyInfo[X] {
  def enabled = propertyInfo.enabled

  def mandatory = propertyInfo.mandatory

  def clazz = propertyInfo.clazz

  def name = propertyInfo.name

  override def extension[T](extClass: Class[T]): Option[T] = {
    super.extension(extClass) match {
      case Some(ext) => Some(ext)
      case None => propertyInfo.extension(extClass)
    }
  }
}